package net.jeeframework.fileuplod;

import java.io.File;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;

public class FileInfo implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5090957256497035375L;

	public static final long K = 1024;

	public static final long M = 1024 * 1024;

	/**
	 * 
	 */
	private String fileId;
	/**
	 * 
	 */
	private String filename;
	/**
	 * 
	 */
	private String extension;
	/**
	 * 
	 */
	private String path;
	/**
	 * 
	 */
	private long fileSize;
	/**
	 * 
	 */
	private String contentType;

	public FileInfo(String filename) {
		super();
		this.filename = filename;

		build();
	}

	public FileInfo(String filename, long fileSize, String contentType) {
		super();
		this.filename = filename;
		this.fileSize = fileSize;
		this.contentType = contentType;

		build();
	}

	public FileInfo(String fileId, String filename, String extension, String path, long fileSize, String contentType) {
		super();
		this.fileId = fileId;
		this.filename = filename;
		this.extension = extension;
		this.path = path;
		this.fileSize = fileSize;
		this.contentType = contentType;

		build();
	}

	// ------------------ helper ------------------- //

	private String getUniqueId() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	private String getDateFormatPath() {
		DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
		return fmt.format(new Date());
	}

	private String format(String path) {
		return path.replace("\\", "/").replace("//", "/");
	}

	private void build() {
		this.extension = FilenameUtils.getExtension(this.filename);
		this.filename = getUniqueId() + "." + extension;
		this.fileId = format(getDateFormatPath() + File.separator + this.filename);
	}

	// ------------------ getter and setter ------------------- //
	public String getFileId() {
		return fileId;
	}

	public void setFileId(String fileId) {
		this.fileId = fileId;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getExtension() {
		return extension;
	}

	public void setExtension(String extension) {
		this.extension = extension;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	@Override
	public String toString() {
		return "FileInfo [fileId=" + fileId + ", filename=" + filename + ", extension=" + extension + ", path=" + path + ", fileSize=" + fileSize + ", contentType=" + contentType + "]";
	}

}
